package com.wyp.didishige.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wyp.didishige.activity.users.UserInfoActivity;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.model.Request;
import com.wyp.didishige.model.User;
import com.wyp.didishige.utils.DateUtil;
import com.wyp.didishige.utils.ImageLoadOptions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by horry on 2015/4/23.
 */
public class RequestAdapter extends BaseAdapter {
    List<Request> mList;
    Context mContext;

    public RequestAdapter(Context context, List<Request> list) {
        this.mList = list;
        this.mContext = context;
    }

    public void appendToList(List<Request> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        mList.removeAll(mList);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position--);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.request_item, null);
            holder.item_text = (TextView) convertView.findViewById(R.id.item_text);
            holder.item_time = (TextView) convertView.findViewById(R.id.item_time);
            holder.request_status = (ImageView) convertView.findViewById(R.id.request_status);
            holder.shiGe_head = (ImageView) convertView.findViewById(R.id.shiGe_head);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Request item = mList.get(position);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = new Date();
        try {
            date = simpleDateFormat.parse(item.getCreatedAt());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        if (item.isCancel()) {
            holder.request_status.setImageResource(R.drawable.tag_cancel);
        } else if (item.isEvaluate()) {
            holder.request_status.setImageResource(R.drawable.tag_complete);
        } else {
            holder.request_status.setImageResource(R.drawable.tag_ing);
        }
        holder.item_time.setText(DateUtil.getDateSpan(calendar.getTimeInMillis() / 1000,
                DateUtil.getTimestamp(new Date())));
        holder.item_text.setText(item.getTitle());
        final User shiGe = item.getShiGeObject();
        if (shiGe != null && shiGe.getAvatar() != null) {
            ImageLoader.getInstance().displayImage(shiGe.getAvatar(), holder.shiGe_head, ImageLoadOptions.getOptions());
            holder.shiGe_head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString(Config.KEY_USER_ID, shiGe.getObjectId());
                    IntentUtil.start_activity((Activity) mContext, UserInfoActivity.class, bundle);
                }
            });
        } else {
            holder.shiGe_head.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        public TextView item_time;
        public TextView item_text;
        public ImageView request_status;
        public ImageView shiGe_head;
    }
}
